﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using UITLibraryManagement.Utilities;

namespace UITLibraryManagement.ModelValidators
{
    public class DocGiaValidator : DOCGIA, IDataErrorInfo
    {        
        public DocGiaValidator(DOCGIA docgia) : base(docgia)
        {
        }

        static readonly string[] ValidatedProperties = 
        {
            "TenDocGia",
            "DienThoai",
            "SoCMND",
            "MaDocGia"
        };

        /// <summary>
        /// Returns true if this object has no validation errors.
        /// </summary>
        public bool IsValid
        {
            get
            {
                foreach (string property in ValidatedProperties)
                    if (GetValidationError(property) != null)
                        return false;

                return true;
            }
        }

        #region Members
        public string Error
        {
            get
            {
                StringBuilder error = new StringBuilder();

                // iterate over all of the properties
                // of this object - aggregating any validation errors
                PropertyDescriptorCollection props = TypeDescriptor.GetProperties(this);
                foreach (PropertyDescriptor prop in props)
                {
                    String propertyError = this[prop.Name];
                    if (propertyError != string.Empty && propertyError != null)
                    {
                        error.Append((error.Length != 0 ? ", " : "") + propertyError);
                    }
                }

                return error.Length == 0 ? null : error.ToString();
            }
        }
        //public string Error
        //{
        //    get {
        //        StringBuilder error = new StringBuilder();

        //        // iterate over all of the properties
        //        // of this object - aggregating any validation errors
        //        PropertyDescriptorCollection props = TypeDescriptor.GetProperties(this);
        //        foreach (PropertyDescriptor prop in props)
        //        {
        //            String propertyError = this[prop.Name];
        //            if (propertyError != string.Empty)
        //            {
        //                error.Append((error.Length != 0 ? ", " : "") + propertyError);
        //            }
        //        }

        //        return error.Length == 0 ? null : error.ToString();
        //    }
        //}
        public string this[string columnName]
        {
            get { return GetValidationError(columnName); }
        }
        #endregion

        #region Validation
        public string GetValidationError(string propertyName)
        {
            if (Array.IndexOf(ValidatedProperties, propertyName) < 0)
                return null;
            string error = null;
            switch (propertyName)
            {
                case "TenDocGia":
                    error = this.ValidateTenDocGia();
                    break;
                case "DienThoai":
                    error = this.ValidateDienThoai();
                    break;
                case "SoCMND":
                    error = this.ValidateCMND();
                    break;
                case "MaDocGia":
                    error = this.ValidateMaDocGia();
                    break;
                default:
                    break;
            }
            return error;
        }

        private string ValidateMaDocGia()
        {
            if(!StringUtility.IsNumber(this.MaDocGia))
                return CONSTANTS.ERROR_MADOCGIA_INVALID;
            return String.Empty;
        }

        public string ValidateTenDocGia()
        {
            if (StringUtility.IsStringMissing(this.TenDocGia))
                return CONSTANTS.ERROR_MISSING_NAME;
            return String.Empty;
        }
        public string ValidateDienThoai()
        {
            if (!StringUtility.IsValidPhoneNumber(this.DienThoai))
                return CONSTANTS.ERROR_PHONENUMBER_INVALID;
            return String.Empty;
            
        }
        public string ValidateCMND()
        {
            if (!StringUtility.IsValidCMND(this.SoCMND))
                return CONSTANTS.ERROR_CMND_INVALID;
            return String.Empty;
        }
        #endregion

        #region Properties

        #endregion
    }
}
